<template>
  <el-container>
    <el-header>
      <el-row class="visitandpiont">
        <el-col>
          <el-button :class="{ cur: tab === 1 }" @click="tab = 1" class="buttonp2">
            <h3>预约记录</h3>
          </el-button>
        </el-col>
        <el-col>
          <el-button trigger="hover" :class="{ cur: tab === 2 }" @click="tab = 2" class="buttonp1">
            <h3>访客记录</h3>
          </el-button>
        </el-col>
      </el-row>
    </el-header>
    <el-main>
      <div class="today" v-show="tab === 1">
        <el-row style="text-align: end">
          <el-button @click="dialogFormVisible = true" class="buttonb">新建</el-button>
        </el-row>

        <el-row class="piontment">
          <el-col>
            <h3>今日预约数：{{sub_total}}</h3>
          </el-col>
        </el-row>
        <el-row class="underpiontment">
          <el-col class="way">
            <h6>预约方式</h6>
          </el-col>
          <el-col class="drawway">
            <el-select v-model="formInline.region0">
              <el-option label="微信预约" value="微信预约"></el-option>
              <el-option label="公众号预约" value="公众号预约"></el-option>
            </el-select>
          </el-col>
          <el-col class="state">
            <h6>处理状态</h6>
          </el-col>
          <el-col class="redrawway">
            <el-select v-model="formInline.region1">
              <el-option label="待处理" value="0"></el-option>
              <el-option label="已处理" value="1"></el-option>
            </el-select>
          </el-col>
          <el-col class="input">
            <el-input v-model="formInline.user" placeholder="输入关键字"></el-input>
          </el-col>
          <el-col class="find">
            <el-button type="primary" @click="findBy_sub">查询</el-button>
          </el-col>
        </el-row>

        <el-row class="underpiontment">
          <el-col class="piontmenttime">
            <h6>预约时间</h6>
          </el-col>
          <el-col class="drawwayblock">
            <div class="block">
              <el-date-picker
                v-model="value"
                type="daterange"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="['00:00:00', '23:59:59']"
              ></el-date-picker>
            </div>
          </el-col>

          <el-col class="pointime">
            <h6>预计到访时间</h6>
          </el-col>
          <el-col class="redrawwayblock">
            <div class="block">
              <el-date-picker
                v-model="value1"
                type="daterange"
                start-placeholder1="开始日期"
                end-placeholder1="结束日期"
                :default-time="['00:00:00', '23:59:59']"
              ></el-date-picker>
            </div>
          </el-col>
        </el-row>
        <el-row class="table">
          <el-table :data="tableData_sub" style="width: 100%">
            <el-table-column prop="id" label="编号">
              <template slot-scope="scope">
                <span>{{ scope.$index + 1 }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="预约人员"></el-table-column>
            <el-table-column prop="subscribleTime" label="预约时间"></el-table-column>
            <el-table-column prop="reason" label="预约事由"></el-table-column>
            <el-table-column prop="timeOfArrival" label="预计到访时间"></el-table-column>
            <el-table-column prop="subscribeWay" label="预约方式"></el-table-column>
            <el-table-column prop="status" label="状态"></el-table-column>
            <el-table-column width="260" label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  @click="handleEdit(scope.$index, scope.row),(dialogFormVisible1 = true)"
                >编辑</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row)"
                >删除</el-button>
                <el-dialog :visible.sync="dialogFormVisible">
                  <el-form :model="form" class="dialog-body">
                    <el-form-item label="预约人员" :label-width="formLabelWidth">
                      <el-input v-model="form.name" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="预约事由" :label-width="formLabelWidth">
                      <el-input clearable v-model="form.name1" autocomplete="off"></el-input>
                    </el-form-item>

                    <el-form-item label="到访时间" :label-width="formLabelWidth">
                      <div class="block">
                        <el-date-picker v-model="value_new" type="datetime" placeholder="选择日期时间"></el-date-picker>
                      </div>
                    </el-form-item>
                    <el-form-item label="预约方式" :label-width="formLabelWidth">
                      <el-select v-model="formInline.region_new">
                        <el-option label="微信预约" value="微信预约"></el-option>
                        <el-option label="公众号预约" value="公众号预约"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-form>
                  <div slot="footer">
                    <el-button class="dialog-footer1" @click="dialogFormVisible = false">取 消</el-button>
                    <el-button class="dialog-footer2" type="primary" @click="add">确 定</el-button>
                  </div>
                </el-dialog>
                <el-dialog :visible.sync="dialogFormVisible1">
                  <el-form :model="form" class="dialog-body">
                    <el-form-item label="预约人员" :label-width="formLabelWidth">
                      <el-input v-model="form.name3" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="预约事由" :label-width="formLabelWidth">
                      <el-input clearable v-model="form.name4" autocomplete="off"></el-input>
                    </el-form-item>

                    <el-form-item label="到访时间" :label-width="formLabelWidth">
                      <div class="block">
                        <el-date-picker v-model="value_update" type="datetime" placeholder="选择日期时间"></el-date-picker>
                      </div>
                    </el-form-item>

                    <el-form-item label="预约方式" :label-width="formLabelWidth">
                      <el-select v-model="formInline.region_update">
                        <el-option label="微信预约" value="微信预约"></el-option>
                        <el-option label="公众号预约" value="公众号预约"></el-option>
                      </el-select>
                    </el-form-item>

                    <el-form-item label="状态" :label-width="formLabelWidth">
                      <el-select v-model="formInline.region_update_status">
                        <el-option label="待处理" value="0"></el-option>
                        <el-option label="已处理" value="1"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-form>
                  <div slot="footer">
                    <el-button class="dialog-footer1" @click="dialogFormVisible1 = false">取 消</el-button>
                    <el-button class="dialog-footer2" type="primary" @click="update">确 定</el-button>
                  </div>
                </el-dialog>
              </template>
            </el-table-column>
          </el-table>
        </el-row>
      </div>
      <div class="todayvisit" v-show="tab === 2">
        <el-row class="piontment">
          <el-col>
            <h3>今日到访数：{{visit_total}}</h3>
          </el-col>
        </el-row>
        <el-row class="underpiontment">
          <el-col class="way">
            <h6>预约方式</h6>
          </el-col>
          <el-col class="drawway">
            <el-select v-model="formInline.region2">
              <el-option label="微信预约" value="微信预约"></el-option>
              <el-option label="公众号预约" value="公众号预约"></el-option>
            </el-select>
          </el-col>
          <!--          <el-col class="state">-->
          <!--            <h6>处理状态</h6>-->
          <!--          </el-col>-->
          <!--          <el-col class="redrawway">-->
          <!--            <el-select v-model="formInline.region3">-->
          <!--              <el-option label="已处理" value="shanghai"></el-option>-->
          <!--              <el-option label="区域二" value="beijing"></el-option>-->
          <!--            </el-select>-->
          <!--          </el-col>-->
        </el-row>

        <el-row class="underpiontment">
          <el-col class="piontmenttime">
            <h6>到访时间</h6>
          </el-col>
          <el-col class="drawwayblock">
            <div class="block">
              <el-date-picker
                v-model="value2"
                type="daterange"
                start-placeholder2="开始日期"
                end-placeholder2="结束日期"
                :default-time="['00:00:00', '23:59:59']"
              ></el-date-picker>
            </div>
          </el-col>

          <el-col class="input1">
            <el-input v-model="formInline.user_visit" placeholder="输入关键字"></el-input>
          </el-col>
          <el-col class="find">
            <el-button type="primary" @click="findBy_Visit">查询</el-button>
          </el-col>
        </el-row>
        <el-row class="table">
          <el-table :data="tableData_Visitor" style="width: 100%">
            <el-table-column prop="id" label="编号">
              <template slot-scope="scope">
                <span>{{ scope.$index + 1 }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="到访人员"></el-table-column>
            <el-table-column prop="subscribeWay" label="预约方式"></el-table-column>
            <el-table-column prop="visitTime" label="到访时间"></el-table-column>
            <el-table-column prop="visitReason" label="到访事由"></el-table-column>
            <el-table-column prop="leaveTime" label="离开时间"></el-table-column>
          </el-table>
        </el-row>
      </div>
    </el-main>
  </el-container>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      formInline: {
        user: "",
        region: "",
      },
      sub_total: 0, //记录今日预约总数
      visit_total: 0, //记录今日访客总数
      //sub_status:"",//记录预约记录的状态
      value: "", //日期选择
      value1: "",
      value2: "", //日期选择
      value_new: "",
      value_update: 0,
      dialogFormVisible: false,
      dialogFormVisible1: false,
      form: {
        name: "",
        name1: "",
        name2: "",
        name3: "",
        name4: "",
        name5: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      formLabelWidth: "80px",
      tableData_Visitor: [
        {
          id: 1,
          name: 1,
          subscribeWay: 1,
          visitTime: 1,
          visitReason: 2,
          leaveTime: 2,
        },
      ],
      tableData_sub: [
        {
          id: 1,
          name: 1,
          subscribleTime: 1,
          reason: 1,
          timeOfArrival: 1,
          subscribeWay: 1,
          status: 1,
        },
      ],
      tab: 1,
    };
  },
  computed: {
    scId() {
      return this.$store.state.scId;
    },
  },
  methods: {
    getdate(date) {
      let now = date,
        y = now.getFullYear(),
        m = now.getMonth() + 1,
        d = now.getDate();
      return (
        y +
        "-" +
        (m < 10 ? "0" + m : m) +
        "-" +
        (d < 10 ? "0" + d : d) +
        " " +
        now.toTimeString().substr(0, 8)
      );
    },
    get_nowdate(date) {
      let now = date,
        y = now.getFullYear(),
        m = now.getMonth() + 1,
        d = now.getDate();
      return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d);
    },

    async getSubscribeFindAll() {
      await this.$axios({
        methods: "get",
        url: `api/${this.scId}/Subscribe/findAll`,
      })
        .then((res) => {
          let data = res.data.data;
          this.tableData_sub = data;
          this.sub_total = 0;
          var now_date = new Date();
          for (let i = 0; i < this.tableData_sub.length; i++) {
            //记录今日的预约数
            if (
              this.get_nowdate(now_date) ==
              this.get_nowdate(new Date(data[i].subscribleTime))
            ) {
              this.sub_total++;
            }
            //根据后端传来的状态进行筛选展示
            if (data[i].status == 1) {
              this.tableData_sub[i].status = "已处理";
            } else {
              this.tableData_sub[i].status = "待处理";
            }
            //把时间戳改yy-mm-dd的形式
            this.tableData_sub[i].subscribleTime = this.getdate(
              new Date(data[i].subscribleTime)
            );
            this.tableData_sub[i].timeOfArrival = this.getdate(
              new Date(data[i].timeOfArrival)
            );
          }
          //console.log(data)
        })
        .catch((err) => {
          this.$message.error("请求失败");
          console.log(err);
        });
    },

    async getVisitFindAll() {
      await this.$axios({
        methods: "get",
        url: `api/${this.scId}/Visitor/findAll`,
      })
        .then((res) => {
          let data = res.data.data;
          this.tableData_Visitor = data;
          this.visit_total = 0;
          var now_date = new Date();
          for (let i = 0; i < this.tableData_Visitor.length; i++) {
            //记录今日的访客数
            if (
              this.get_nowdate(now_date) ==
              this.get_nowdate(new Date(data[i].visitTime))
            ) {
              this.visit_total++;
            }
            this.tableData_Visitor[i].visitTime = this.getdate(
              new Date(data[i].visitTime)
            );
            this.tableData_Visitor[i].leaveTime = this.getdate(
              new Date(data[i].leaveTime)
            );
          }
          console.log(data);
        })
        .catch((err) => {
          this.$message.error("请求失败");
          console.log(err);
        });
    },

    async findBy_sub() {
      if (this.value == null) {
        console.log("null"); //meizhixing
      } else {
        console.log(this.value[0]);
        this.$axios({
          url: `/api/${this.scId}/Subscribe/findByDynamic_sub`,
          method: "post",
          data: {
            subscribe_way: this.formInline.region0,
            status: this.formInline.region1,
            name: this.formInline.user,
            // subscribe_way: null,
            // status: null,
            sub_start: this.value[0],
            sub_end: this.value[1],
            arrival_start: this.value1[0],
            arrival_end: this.value1[1],
            //name: this.formInline.user,
          },
        }).then((res) => {
          console.log(res);
          let data = res.data.data;
          console.log(data);
          this.tableData_sub = data;
          for (let i = 0; i < this.tableData_sub.length; i++) {
            //根据后端传来的状态进行筛选展示
            if (data[i].status == 1) {
              this.tableData_sub[i].status = "已处理";
            } else {
              this.tableData_sub[i].status = "待处理";
            }
            this.tableData_sub[i].subscribleTime = this.getdate(
              new Date(data[i].subscribleTime)
            );
            this.tableData_sub[i].timeOfArrival = this.getdate(
              new Date(data[i].timeOfArrival)
            );
          }
        });
      }
      //  this.getSubscribeFindAll()
    },
    async findBy_Visit() {
      if (this.value2 == null) {
        console.log("null");
      } else {
        console.log(this.formInline.region2);
        this.$axios({
          url: `/api/${this.scId}/Visitor/findByDynamic`,
          method: "post",
          data: {
            subscribe_way: this.formInline.region2,
            visit_start: this.value2[0],
            visit_end: this.value2[1],
            name: this.formInline.user_visit,
          },
          headers: {
            "Content-Type": "application/json",
          },
        }).then((res) => {
          let data = res.data.data;
          console.log(data);
          this.tableData_Visitor = data;
          for (let i = 0; i < this.tableData_Visitor.length; i++) {
            this.tableData_Visitor[i].visitTime = this.getdate(
              new Date(data[i].visitTime)
            );
            this.tableData_Visitor[i].leaveTime = this.getdate(
              new Date(data[i].leaveTime)
            );
          }
        });
      }
    },
    async add() {
      this.$axios({
        url: `/api/${this.scId}/Subscribe/add`,
        method: "post",
        data: {
          name: this.form.name,
          reason: this.form.name1,
          timeOfArrival: this.value_new,
          subscribeWay: this.formInline.region_new,
          status: 0,
        },
        headers: {
          "Content-Type": "application/json",
        },
      }).then((res) => {
        console.log(res);
        this.getSubscribeFindAll();
      });
      this.dialogFormVisible = false;
    },
    async delete(id) {
      this.$axios({
        url: `/api/${this.scId}/Subscribe/delete`,
        method: "post",
        data: {
          id: id,
        },
        headers: {
          "Content-Type": "application/json",
        },
      }).then((res) => {
        console.log(res);
        this.getSubscribeFindAll();
      });
    },
    async update() {
      this.$axios({
        url: `/api/${this.scId}/Subscribe/update`,
        method: "post",
        data: {
          id: this.form.id,
          name: this.form.name3,
          reason: this.form.name4,
          timeOfArrival: this.value_update,
          subscribeWay: this.formInline.region_update,
          status: this.formInline.region_update_status,
        },
        headers: {
          "Content-Type": "application/json",
        },
      }).then((res) => {
        console.log(res);
        this.getSubscribeFindAll();
      });
      this.dialogFormVisible1 = false;
    },
    onSubmit() {
      console.log("submit!");
    },
    handleEdit(index, row) {
      console.log(index, row);
      this.form.id = row.id;
      this.form.name3 = row.name;
      this.form.name4 = row.reason;
      //this.formInline.region_update =row.subscribeWay
    },
    handleDelete(index, row) {
      console.log(index, row);
      this.delete(row.id);
    },
  },
  components: {},
  mounted() {
    this.getSubscribeFindAll();
    this.getVisitFindAll();
  },
  watch: {
    scId: {
      handler() {
        this.getSubscribeFindAll();
        this.getVisitFindAll();
      },
      immediate: true,
      deep: true,
    },
  },
};
</script>

<style scoped>
.el-header {
  background-color: white;
  width: 291px;
  margin: 0px 20px;
  padding: 0;
}
.visitandpiont {
  display: flex;
  margin-left: 0%;
}
.piontment {
  width: 350px;
  height: 69px;
  border: solid 1px;
}
.underpiontment {
  width: 1400px;
  height: 60px;
  margin: 20px 0px;
  display: flex;
}
.way {
  width: 100px;
}
.drawway {
  width: 300px;
  padding: 10px 15px;
}
.redrawway {
  width: 300px;
  margin: 0px 10px;
  padding: 10px 0px;
}
.state {
  width: 100px;
}
.input {
  width: 300px;

  padding: 10px 0px;
}
.input1 {
  width: 300px;
  margin-left: 100px;
  padding: 10px 0px;
}
.find {
  width: 70px;
  height: 70px;
  margin: 0px 60px;
  padding: 10px 0px;
}
.buttonp1 {
  height: 63px;
  width: 146px;
  padding: 9px 40px;
  display: flex;
}
.buttonp2 {
  height: 63px;
  width: 146px;
  padding: 9px 40px;
  display: flex;
}
.cur {
  background-color: gray;
}

.pointime {
  width: 300px;
}
.drawwayblock {
  width: 280px;
  margin: 0px 30px;
  padding: 10px 15px;
}
.piontmenttime {
  width: 90px;
}
.redrawwayblock {
  width: 280px;
  margin: 0px -80px;
  padding: 10px 15px;
}

.el-main {
  background-color: white;
  text-align: center;
  margin: 0px 20px;
}
</style>
